/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.system.biz.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.system.biz.BankBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.UserRateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @author yangpeng
 * Created by on 2024-01-30 13:04
 */
@Service
public class BankBizServiceImpl extends BaseBizServiceImpl implements BankBizService {

    @Resource
    private BankService bankService;
    @Resource
    private UserRateService userRateService;
    @Resource
    private ISysUserService userService;

    @Override
    public void save(Bank bank) {
        this.setBank(bank);
        if (bank.getId() != null) {
            bankService.updateDomain(bank);
        } else {
            bankService.addDomain(bank);
        }

    }

    private void setBank(Bank bank) {
        userRateService.checkUserRate(bank.getUserId(), bank.getChannelCode());
        bank.setCardNo(StrUtil.trim(bank.getCardNo()));
        String channelCode = bank.getChannelCode();
        bank.setChannelName(ChannelEnum.getName(channelCode));
        if (StrUtil.equals(bank.getChannelCode(), ChannelEnum.TB.getCode())) {//如果是淘宝
            if (bank.getMaxAmount() == null) {
                throw new BusiException("请输入限制金额");
            }
            String index = "http";
            String result = index + StrUtil.subAfter(bank.getCardNo(), index, true);
            String url = null;
            String[] split = result.split(" ");
            for (int i = 0; i < split.length; i++) {
                url = split[i];
                if (StrUtil.isNotBlank(url) &&
                        (StrUtil.contains(url, "https:")
                                || StrUtil.contains(url, "http:"))) {

                    bank.setCardNo(url);
                    break;
                }
            }

            if (StrUtil.isBlank(url)) {
                throw new BusiException("请添加正确的商品链接");
            }

        }


        if (StrUtil.isNotBlank(bank.getZfbUserId())) {
            String replace = StrUtil.replace(bank.getZfbUserId(), " ", "");

            bank.setZfbUserId(replace);
        }
        try {
            //小荷包
            if (StrUtil.contains(bank.getType(), "zfb") && StrUtil.isNotBlank(bank.getBankName())) {
                InputStream inputStreamFromUrl = QRCodeUtil.getInputStreamFromUrl(bank.getBankName());
                String read = QRCodeUtil.read(inputStreamFromUrl);
                if (StrUtil.isBlank(read)) {
                    throw new BusiException("请上传正确的支付宝二维码");
                }
                bank.setZfbUrl(read);
            }
        } catch (Exception e) {
            throw new BusiException("请上传正确的支付宝二维码");
        }


    }


}